#include "savemanager.h"
#include "QStringList"
#include "QFile"
#include "errormanager.h"

SaveManager* SaveManager::instance;
QStringList bindings;

QString userName = "";
QString password = "";
double volume = 50.0;
bool skypeBot = false;


SaveManager::SaveManager()
{

}

void SaveManager::LoadConfigs(){

    QFile f("configs.cfg");
    f.open(QIODevice::ReadOnly);
    QString content = f.readAll();
    f.close();

    QStringList list = content.split(",");

    int index = 0;

    if(list.length() != 4)
    {
        return;
    }

    bool empty = true;

    foreach(QString element, list){
        if(!element.isEmpty()){
            empty = false;
            break;
        }

    }

    if(empty) return;


    foreach(QString cfg, list)
    {
        switch(index)
        {

        case 0:
            volume = cfg.toDouble();
            break;
        case 1:
            userName = cfg;
            break;
        case 2:
            password = cfg;
            break;
        case 3:
            skypeBot = (bool)cfg.toInt();
            break;
        }
        index++;
    }
}

bool SaveManager::BindingsOK()
{
    QStringList temporary = bindings;
    return (temporary.removeDuplicates()== 0 && (!temporary.contains("CTRL,") && !temporary.contains("ALT,") && !temporary.contains("SHIFT,") && !temporary.isEmpty() && !temporary.contains(",")));
}

void SaveManager::ClearBindings(){
    bindings.clear();
}

void SaveManager::AddBindings(QString &s)
{
    if(bindings.size()==5)
        bindings.clear();

    bindings.append(s);

}

void SaveManager::SaveConfigs()
{
    QString path = "configs.cfg";
    QFile f(path);
    f.open( QIODevice::WriteOnly );
    QString content;
    content.append(QString::number(volume) + ",");
    content.append(userName + ",");
    content.append(password + ",");
    if(skypeBot)
        content.append("1");
    else
        content.append("0");

    f.write(content.toUtf8());
    f.close();
}

void SaveManager::SaveBindings(QString path)
{
    QFile f(path);
    f.open( QIODevice::WriteOnly );
    QString content;

    foreach(QString binding, bindings)
    {
        content.append(binding.append(","));
    }

    content.truncate(content.length()-1);
    f.write(content.toUtf8());
    f.close();

}

QStringList SaveManager::GetBindings()
{
    return bindings;
}

SaveManager *SaveManager::GetInstance()
{
    if(instance == NULL)
        instance = new SaveManager();


    return instance;
}

QString SaveManager::GetPassword(){
    return password;
}

QString SaveManager::GetUserName(){
    return userName;
}
double SaveManager::GetVolume(){
    return volume;
}
void SaveManager::SetPassword(QString pass){
    password = pass;
}

void SaveManager::SetUserName(QString user){
    userName = user;
}
void SaveManager::SetVolume(double vol){
    volume = vol;
}
bool SaveManager::GetSkypeBot(){
    return skypeBot;
}
void SaveManager::SetSkypeBot(bool bot){
    skypeBot = bot;
}
